package cn.itsource.hrm.exception;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 断言类：断言某个条件，如果不满足就报BusinessException
 */
public class Assert {
    //手机号的验证规则
    private static final String PHONE_REGEX = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0-9]))\\d{8}$";

    //邮箱的校验规则
    private static final String EMAIL_REGEX = "[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?";
    //断言验证手机号
    public static void assertPhone(String phone,String message){
        //格式判断
        Pattern p = Pattern.compile(PHONE_REGEX);
        Matcher m = p.matcher(phone);
        if(!m.matches()){
            throw new BusinessException(message);
        }
    }

    public static void assertPhone(String phone){
        assertPhone(phone,"无效的手机号");
    }
    //断言验证邮箱
    public static void assertEmail(String email,String message){
        //格式判断
        Pattern p = Pattern.compile(EMAIL_REGEX);
        Matcher m = p.matcher(email);
        if(!m.matches()){
            throw new BusinessException(message);
        }
    }
    public static void assertEmail(String email){
        assertEmail(email,"无效的邮箱");
    }

    /**
     *  断言不为null
     * @param obj 用来判断的对象
     * @param message 提示信息
     */
    public static void assertNotNull(Object obj,String message){
        if (obj==null)
            throw  new BusinessException(message);
    }
    /** 断言为null
     * @param obj 用来判断的对象
     * @param message 提示信息
     */
    public static void assertNull(Object obj,String message){
        if (obj!=null)
            throw  new BusinessException(message);
    }
    /**
     *  非空断言
     * @param str 用来判断的对象
     * @param message 提示信息
     */
    public static void assertNotEmpty(String str,String message){
        if (str==null || str.trim().equals(""))
            throw  new BusinessException(message);
    }


    /**
     *  断言list不为null
     * @param objs
     * @param message
     */
    public static void assertListNotNull(List<?> objs, String message){
        if (objs==null || objs.size()<1)
            throw  new BusinessException(message);
    }
    /**
     *  断言list为null
     * @param objs
     * @param message
     */
    public static void assertListNull(List<?> objs, String message){
        if (objs!=null && objs.size()>0)
            throw  new BusinessException(message);
    }

    /**
     *  断言list为null
     * @param objs
     * @param errorCode 带错误码提示信息
     */
    public static void assertListNull(List<?> objs, ErrorCode errorCode){
        if (objs!=null && objs.size()>0)
            throw  new BusinessException(errorCode);
    }
    /**
     *  断言数组不为null
     * @param objs
     * @param message
     */
    public static void assertArrayNotNull(Object[] objs, String message){
        if (objs==null || objs.length<1)
            throw  new BusinessException(message);
    }
    /**
     *  断言数组不为null
     * @param objs
     * @param message
     */
    public static void assertArrayNull(Object[] objs, String message){
        if (objs!=null && objs.length>0)
            throw  new BusinessException(message);
    }

    /**
     * 断言为true
     * @param b
     * @param message
     */
    public static void assertTrue(boolean b, String message){
        if (!b)
            throw  new BusinessException(message);
    }

    /**
     * 断言为true
     * @param b
     * @param message
     */
    public static void assertFalse(boolean b, String message){
        if (b)
            throw  new BusinessException(message);
    }


}
